package hxg.thread;

import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * @author HeXiangGuang
 * @create 2022-10-16 17:13
 */
public class TestDemo implements Callable{

    @Override
    public Object call() throws Exception {
        int sum=0;
        for(int i=0;i<10;i++){
            sum=sum+i;
            Thread.sleep(300);
        }
        System.out.println("sum:"+sum);
        return sum;
    }
    public static void main(String args[]) throws Exception{

        System.out.println("time1:"+new Date());
        TestDemo d = new TestDemo();
        FutureTask futureTask = new FutureTask(d);
        Thread thread = new Thread(futureTask);
        thread.start();
        System.out.println("time2:"+new Date());
        //参数一设置任务超时时间，二设置任务超时时间的具体单位。
        Object sum = futureTask.get(5, TimeUnit.SECONDS);
        System.out.println("time3:"+new Date());
        System.out.println(sum);

    }
}


